(function(win){
    /**
     * 空判断
     * @param val
     * @returns
     */
    function isEmpty(val){
        if(val == null || val == undefined ||  val === "" || val.toString().length === 0){
            return true;
        }else{
            return false;
        }
    };
    
    /**
     * 判断空对象
     * @param obj
     * @returns
     */
    function isEmptyObj(obj){
		for(var key in obj){
			return false;
		}
		return true;
	}
    
    /**
     * json对象格式化--》a=1&b=1  用于axios post数据发送
     * @param json
     * @returns
     */
    function qsStringify(json){
    	if(isEmptyObj(json))return ""
		var stringJson = ""
		for(var key in json){
			if(!isEmpty(stringJson)){
				stringJson += "&" + key +"="+ json[key]
			}else{
				stringJson += key +"="+ json[key]
			}
		}
		return stringJson
	}
    
    /**
     * 清除所有空格
     * @param str
     * @returns
     */
    function trimAll(str){
        var reg = new RegExp(" ","g");
        return str.replace(reg,"");
    };
    
    function replaceAll(str,rep,rep2){
        var reg = new RegExp(rep,"g");
        return str.replace(reg,rep2);
    };

    /**
     * 显示弹窗
     * @param text
     * @returns
     */
    function showTip(text){
        if($("#tip").length == 0){
            $("body").append("<div id='tip'></div>");
        };
        var tipDom = $("#tip");
        if(!isEmpty(text)){
            text = trimAll(text);
            tipDom.stop(true,false).text(text).css({
                "margin-top":-tipDom.outerHeight() / 2,
                "margin-left":-tipDom.outerWidth() / 2
            }).fadeIn(300).delay(1500).fadeOut(300);
        }
    };
    
    /**
     * 图片等比例缩放
     * @param imgW
     * @param imgH
     * @param maxWidth
     * @param maxHeight
     * @returns json
     */
    function imgFormat(imgW,imgH,maxWidth,maxHeight){
        var _width,_height;
        if(maxWidth / maxHeight <= imgW / imgH){
            _width = maxWidth;  
            _height = maxWidth * (imgH / imgW); 
        }else{
            _width = maxHeight * (imgW / imgH);  
            _height = maxHeight;  
        }
        if(imgW < maxWidth && imgH < maxHeight){
            _width = imgW;
            _height = imgH;
        }
        return {
            "imgWidth":_width,
            "imgHeight":_height
        }
    };
    
    /**
     * 数组去重
     * @param oldarr
     * @returns newArr
     */
    function unique(oldarr){
        var newArr = [];
        oldarr.forEach(function(obj){
            if(newArr.indexOf(obj) === -1){
                newArr.push(obj);
            }
        });
        return newArr;
    };
    
    /**
     * 匹配图片格式
     * @param type
     * @returns mark
     */
    function isImage(type){
        var mark = false;
        type = type.toLowerCase();
        switch(type){
            case ".jpg":
            case ".jpeg":
            case ".png":
            case ".gif":
                mark = true;
            break;
            default:
                mark = false;
            break;
        }
        return mark;
    };
    
    /**
     * 字节转换
     * @param btyes
     * @returns
     */
    function bytesTosize(btyes){
         if(btyes >= 1024 && btyes < 1048576){
             return (btyes / 1024).toFixed(2) + "KB";
         }else{
             return (btyes / 1024 / 1024).toFixed(2) + "MB";
         }
     };
     
     /**
      * 窗口滚动动画
      * @param A起始位置
      * @param B目标位置
      * @param rate缓动速率
      * @param callback
      * @returns
      */
     function scrollAnimate(A,B,rate,callback){
		if(A == B || typeof A != 'number') {
	        return;    
	    }
		rate = rate || 2;
		B = B || 0;
		var step = function(){
			A = A + (B - A) / rate;
			
			if(Math.abs(A - B) < 1){
				callback(B,true);
				console.log(1)
				return;
			}
			callback(A,false);
			requestAnimationFrame(step);
		}
		step();
	}
    
	/**
	 * 获取图片真实宽度真实高度
	 * @param src
	 * @returns json
	 */
    function getImgWH(src){
    	if(isEmpty(src))return;
    	var image = new Image(),
    		json = {};
    	image.src = src;
    	image.onload = function(){
    		json.w = this.width;
    		json.h = this.height;
    	}
    	return json;
    }
    
    /**
     * 获取鼠标位置
     * @param e
     * @returns
     */
    function getXY(e){
		var ev = e || window.event,x,y;
		ev.preventDefault();
		ev.stopPropagation();
		if(!ev.pageX){
			x = ev.pageX;
			y = ev.pageY;
		}else{
			var _top,_left;
			if(document.body.scrollTop){
				_top = document.body.scrollTop;
				_left = document.body.scrollLeft;
			}else{
				_top = document.documentElement.scrollTop;
				_left = document.documentElement.scrollLeft;
			}
			x = ev.clientX + _left;
			y = ev.clientY + _top;
		}
		return {
			"x":x,
			"y":y
		};
	};
	
	/**
	 * 滚动条
	 * @param boxDom
	 * @param barbox
	 * @param content
	 * @returns
	 */
	var scrollBar = {
		init:function(opts){
			this.xevent(opts);
		},
		setData:function(opts){
			return {
				boxDom : opts.boxDom,//外层容器
				barbox : opts.barbox,//滚动条容器
				contentDom : opts.content,//内容容器
				bar : function(){//滚动按钮
					return this.barbox.children(".bar");
				},
				boxH : function(){//外层容器高度
					return this.boxDom.outerHeight();
				},
				barboxH : function(){//滚动条容器高度
					return this.barbox.height();
				},
				contentH : function(){//内容容器高度
					return this.contentDom.height();
				},
				cHeight : function(){
					return this.contentH() - this.boxH() + 20;//加20上下边距
				},
				boxTop : function(){//盒子距离顶部的距离
					return this.boxDom.offset().top;
				}
			}
		},
		xevent:function(opts){
			var data = this.setData(opts);
			if(data.contentH() < data.boxH()){
				data.bar()[0].onmousedown =  data.barbox[0].onmousedown =  data.boxDom[0].onmouseover = null;
				return;
			}
			var barH = data.boxH() * (data.boxH() / data.contentH()),
				ishide = true,
				isleave = false;
			if(barH <= 40)barH = 40;
			data.bar().animate({"height":barH});
			data.bar()[0].onmousedown = function(e){
				ishide = false;
				var pos = getXY(e),
					that = $(this),
					meHeight = that.height(),//滚动条高度
					maxHeight = data.barboxH() - meHeight,//最大走动位置
					y = pos.y,
					_top = that.position().top;
				document.onmousemove = function(e){
					var pos = getXY(e),
						$y = pos.y - (y - _top);
					if($y <= 0)$y = 0;
					if($y >= maxHeight)$y = maxHeight;
					that.css("top",$y);
					data.contentDom.css("top",- (data.cHeight() * ($y / maxHeight)))
				};
				document.onmouseup = function(){
					this.onmousemove = null;
					this.onmouseup = null;
					ishide = true;
					if(isleave)data.barbox.stop(true).fadeOut(300);
				}
			}

			data.barbox[0].onmousedown = function(e){
				var pos = getXY(e),
					barHeight = data.bar().height(),//滚动条高度
					maxHeight = data.barboxH() - barHeight,//最大走动位置
					y = pos.y - data.boxTop(),
					_top = y - (barHeight / 2);
				if(_top <= 0)_top = 0;
				if(_top >= maxHeight)_top = maxHeight;
				data.bar().css("top",_top);
				data.contentDom.css("top",- data.cHeight() * (_top / maxHeight));
			}
			data.boxDom[0].onmouseleave = function(){
				isleave = true;
			}
			data.boxDom[0].onmouseover = function(){
				isleave = false;
				data.barbox.stop(true).fadeIn(300);
				var _top = data.boxH() / 8;//定义每次滚动的距离				
				this.onmousewheel = function(e){
					var ev = e || window.event,
						pos = event.wheelDelta / 120,
						$top = 0,
						maxHeight = data.barboxH() - data.bar().height();//最大走动位置
					ev.preventDefault()
					if(pos < 0){
						$top = data.bar().position().top + _top;
						if($top >= maxHeight)$top = maxHeight;
						data.bar().css("top",$top);
					}else{
						$top = data.bar().position().top - _top;
						if($top <= 0)$top = 0;
						data.bar().css("top",$top);
					}
					data.contentDom.css("top",- data.cHeight() * ($top / maxHeight));
				}
			}
			data.boxDom[0].onmouseout = function(){
				if(ishide)data.barbox.stop(true).fadeOut(300);
				this.onmousewheel = null;
			};
		}
	}
	
	/**
	 * 单个审核通过、未通过提示
	 * mark:true通过  false 未通过
	 * time:多少毫秒之后显示 不传则直接显示
	 */
	function show_alert_sh(text,mark,time){
		var alertDom = $("#alert_sh"),
			timer = null;
		clearTimeout(timer)
		if(alertDom.length > 0){
			if(mark){
				alertDom.addClass("alert-success").removeClass("alert-danger")
			}else{
				alertDom.addClass("alert-danger").removeClass("alert-success")
			}
			if(time){
				timer = setTimeout(function(){
					clearTimeout(timer)
					alertDom.text(text).css("opacity",0).show()
					.css({
						"margin-left": - alertDom.outerWidth() / 2
					})
					.hide().css("opacity",1).stop(true).fadeIn(200).delay(2000).fadeOut(200)
				},time)
			}else{
				alertDom.text(text).css("opacity",0).show()
				.css({
					"margin-left": - alertDom.outerWidth() / 2
				})
				.hide().css("opacity",1).stop(true).fadeIn(200).delay(2000).fadeOut(200)
			}
		}
	}
	
	/**
	 * 提交审核通过、未通过提示
	 * mark:true通过  false 未通过
	 */
	function show_alert_submit(text,mark){
		var modalDom = $("#alert_submit"),
			okBtn = modalDom.find("button.btn")
		modalDom.modal({
			backdrop: "static",//禁用点击背景关闭
			keyboard: false
		})
		modalDom.find(".modal-body").text(text)
		okBtn[0].onclick = function(){
			if(mark){
				window.location.reload()
			}else{
				modalDom.modal("hide")
			}
		}
		modalDom.modal("show")
	}
	
	/**
	 * confirm弹窗
	 */
	function show_alert_confirm(opt){
		var modalDom = $("#alert_confirm"),
			okBtn = modalDom.find("button.confirm")
		
		if(opt.fn){
			okBtn[0].onclick = function(){
				var $btn = $(this).button('loading')
				opt.fn(modalDom,$btn,function(){
					$btn.button('reset')
					modalDom.modal("hide")
				})
			}
		}
		
		if(opt.text){
			modalDom.find(".modal-body").html(opt.text)
		}
		
		if(opt.height){
			modalDom.find(".modal-body").css("height",opt.height)
		}
		
		modalDom.modal("show")
		
		if(opt.isShowBg){
			$(".modal-backdrop").css("opacity",.5)
		}else{
			$(".modal-backdrop").css("opacity",0)
		}
	}
	
	/**
	 * 图片加载函数
	 * callback 1成功 0失败
	 */
	function imageLoad (src,callback) {
		var image = new Image()
		image.src = src
		
		if (image.complete) {
			callback.call(image, 1)
			return
		}
		
		image.onload = function(){
			callback.call(this, 1)
		}
		
		image.onerror = function(){
			callback.call(this, 0)
		}
	}
	
    window._x = {
        "isEmpty":isEmpty,
        "trimAll":trimAll,
        "imgFormat":imgFormat,
        "showTip":showTip,
        "unique":unique,
        "isImage":isImage,
        "bytesTosize":bytesTosize,
        "isEmptyObj":isEmptyObj,
        "scrollAnimate":scrollAnimate,
        "getImgWH":getImgWH,
        "getXY":getXY,
        "scrollBar":scrollBar,
        "qsStringify":qsStringify,
        "show_alert_sh":show_alert_sh,
        "show_alert_submit":show_alert_submit,
        "show_alert_confirm":show_alert_confirm,
        "replaceAll": replaceAll,
        "imageLoad": imageLoad
    }

})(window)